package br.com.wepa.webapps.orca.logica.negocio.facade;

import java.util.List;

import br.com.wepa.webapps.logger.TraceLogger;
import br.com.wepa.webapps.orca.logica.modelo.Usuario;
import br.com.wepa.webapps.orca.logica.negocio.BusinessException;
import br.com.wepa.webapps.orca.logica.negocio.facade.basic.BasicFacade;
import br.com.wepa.webapps.orca.logica.negocio.facade.to.LoginTO;
import br.com.wepa.webapps.orca.logica.negocio.menu.MontaMenu;
import br.com.wepa.webapps.orca.logica.persistencia.DAOFactory;
import br.com.wepa.webapps.orca.logica.persistencia.UsuarioDAO;
import br.com.wepa.webapps.orca.util.bean.BeanHelperUtil;

class LoginFacade extends BasicFacade<LoginTO> implements LoginFacadeHome {
	private static final TraceLogger logger = new TraceLogger(LoginFacade.class);

	protected UsuarioDAO dao = DAOFactory.instance(DAOFactory.HIBERNATE).getUsuarioDAO();

	public Usuario login(LoginTO to) throws BusinessException {
		logger.entering();
		Usuario usuario = null;
		try {			
			Usuario search = dao.newInstance();
			BeanHelperUtil.copyProperties(search, to);
						
			usuario = dao.findUnique(search);
			
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(),e);
		}
		logger.exiting();
		return usuario;
	}
	
	public List loadMenuItens(LoginTO to) throws BusinessException {
		logger.entering();
		List retorno = null;
		try{
			retorno= new MontaMenu().montaMenu(to.getUsuarioSessao().getCredencial());			
		} catch (Exception e) {
			throw new BusinessException(e.getMessage(),e);
		}
		logger.exiting();
		return retorno;
	}	
}
